home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4744 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: nntp-xfer-2.csn.net!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: random number in C++
  5. Date: 31 Jan 1996 23:12:32 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4eot10$380a@yuma.ACNS.ColoState.EDU>
  8. References: <4eoh03$6jb@pacemaker.cts>
  9. NNTP-Posting-Host: glitch.physics.colostate.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Brian A. Bucher (babucher@mtu.edu) wrote:
  13.  
  14. > Is there _ANY_ way to get a truly random number in a C++ program?
  15. > rand and srand do not generate random numbers, they generate
  16. > pseudo-random numbers, which is what I don't need.
  17.  
  18. > Thanks,
  19. > Boo
  20.  
  21.     Well, OK, no there isn't any way to generate _truly_ random
  22. numbers in _any_ language. A computer is a deterministic system (at
  23. least in theory ;-) and therefore can not generate a truly random
  24. sequence of numbers without special hardware input (white noise,
  25. geiger counter, etc.)
  26.  
  27.     So from this point of view, the answer to your question is no,
  28. it's not possible to get a truly random number sequence... in ANY
  29. language.
  30.  
  31.     However, what you _can_ get from a computer is a "good"
  32. sequence of pseudo-random numbers, "good" meaning
  33. "non-correlated". When people talk about random number generators,
  34. what they really mean is a "pseudo-random number generator with no
  35. statistical correlations in its output".
  36.  
  37.     The problem is that most "system-supplied" random number
  38. generators are, in fact, _NOT_ good random number generators; their
  39. output is correlated. If you're serious about needing a good stream of
  40. randoms, check out "Numerical Recipies in C, 2nd Ed" for an excellent
  41. discussion of what's wrong with most implementations of the rand() and
  42. srand() functions, and some good alternative routines. I personally
  43. use a C++ified version of their ran1() routine; some of the experts
  44. tell me that some "cryptography" routines are even better.
  45.  
  46.                                  /\
  47.                                  \/
  48.  
  49. Dave Steffen                      No, his mind is not for rent
  50. Dept. of Physics                  To any God or Government
  51. Colorado State University         Always hopeful, yet discontent
  52. steffend@lamar.colostate edu      He knows changes aren't permanent-
  53.                       But change is...
  54. "Speak softly...                    
  55. ... and carry a black belt!"              -Neal Peart / RUSH
  56. -----------------------------------------------------------------------
  57.